package com.haolp.sys.service.impl;

import com.haolp.sys.common.exception.ServiceException;
import com.haolp.sys.common.vo.Node;
import com.haolp.sys.dao.SysDeptDao;
import com.haolp.sys.pojo.SysDept;
import com.haolp.sys.service.SysDeptService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author 小李
 * @version 1.0
 * @date 2021/10/13 17:45
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;

    @Override
    public int updateObject(SysDept entity) {
        if (entity == null)
            throw new IllegalArgumentException("修改对象不能为空");
        if (StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("部门名称不能为空");
        int rows = sysDeptDao.updateObject(entity);
        return rows;
    }

    @Override
    public List<Node> doFindZTreeNodes() {
        List<Node> list = sysDeptDao.findZTreeNodes();
        if (list == null || list.size() == 0) throw new SecurityException("没有找到对应的部门");
        return list;
    }

    @Override
    public int saveObject(SysDept entity) {
        if (entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        if (StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("部门名称不能为空");
        int rows = sysDeptDao.insertObject(entity);
        return rows;
    }

    @Override
    public List<Node> findZtreeDeptNodes() {
        List<Node> list = sysDeptDao.findZtreeDeptNodes();
        if (list == null || list.size() == 0)
            throw new SecurityException("没有获取到节点对象");
        return list;
    }

    @Override
    public int deleteObject(Integer id) {
        int rows = sysDeptDao.deleteObject(id);
        if (id == null || id <= 0)
            throw new IllegalArgumentException("没有找到对应的id");
        return rows;
    }

    @Override
    public List<Map<String, Object>> findObjects() {
        List<Map<String, Object>> list = sysDeptDao.findObjects();
        if (list == null || list.size() == 0)
            throw new SecurityException("没有找到对应的部门以及上一级部门信息");
        return list;
    }

    @Override
    public List<Node> findZTreeNodes() {
        List<Node> list = sysDeptDao.findZTreeNodes();
        if (list == null || list.size() == 0) throw new SecurityException("没有找到对应的部门");
        return list;
    }

}
